<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021-3-6
 * Time: 14:22
 */

function get_menu($pid = 0)
{
    $menu = Db::table('system_menu')->where(['pid' => $pid, 'status' => 1])->select();
    foreach ($menu as $k => $m) {
        $menu[$k]['type'] = 1;
        $children         = get_menu($m['id']);
        if (count($children) > 0) {
            $menu[$k]['children'] = $children;
            $menu[$k]['type']     = 0;
        }
    }
    return $menu;
}

function checkHost()
{
    echo 1;
}

/**
 * @param string $path
 * @param array $content
 */
function set_file_con($path, $content)
{
    file_put_contents($path, json_encode($content));
}

function get_file_con($path)
{
    return json_decode(file_get_contents($path), true);
}

function uploadFile($path = '', $fileName = '')
{
    $file = request()->file('file');
    $info = $file->move($path ?: '../uploads', $fileName ?: '');
    if ($info) {
        return $info->getSaveName();
    } else {
        // 上传失败获取错误信息
        echo $file->getError();
    }
}

function getOs()
{
    $os    = '';
    $Agent = $_SERVER['HTTP_USER_AGENT'];
    if (preg_match('win', $Agent) && strpos($Agent, '95')) {
        $os = 'Windows 95';
    } elseif (preg_match('win 9x', $Agent) && strpos($Agent, '4.90')) {
        $os = 'Windows ME';
    } elseif (preg_match('win', $Agent) && ereg('98', $Agent)) {
        $os = 'Windows 98';
    } elseif (preg_match('win', $Agent) && preg_match('nt 5.0', $Agent)) {
        $os = 'Windows 2000';
    } elseif (preg_match('win', $Agent) && preg_match('nt 6.0', $Agent)) {
        $os = 'Windows Vista';
    } elseif (preg_match('win', $Agent) && preg_match('nt 6.1', $Agent)) {
        $os = 'Windows 7';
    } elseif (preg_match('win', $Agent) && preg_match('nt 5.1', $Agent)) {
        $os = 'Windows XP';
    } elseif (preg_match('win', $Agent) && preg_match('nt', $Agent)) {
        $os = 'Windows NT';
    } elseif (preg_match('win', $Agent) && ereg('32', $Agent)) {
        $os = 'Windows 32';
    } elseif (preg_match('linux', $Agent)) {
        $os = 'Linux';
    } elseif (preg_match('unix', $Agent)) {
        $os = 'Unix';
    } else if (preg_match('sun', $Agent) && preg_match('os', $Agent)) {
        $os = 'SunOS';
    } elseif (preg_match('ibm', $Agent) && preg_match('os', $Agent)) {
        $os = 'IBM OS/2';
    } elseif (preg_match('Mac', $Agent) && preg_match('PC', $Agent)) {
        $os = 'Macintosh';
    } elseif (preg_match('PowerPC', $Agent)) {
        $os = 'PowerPC';
    } elseif (preg_match('AIX', $Agent)) {
        $os = 'AIX';
    } elseif (preg_match('HPUX', $Agent)) {
        $os = 'HPUX';
    } elseif (preg_match('NetBSD', $Agent)) {
        $os = 'NetBSD';
    } elseif (preg_match('BSD', $Agent)) {
        $os = 'BSD';
    } elseif (preg_match('OSF1', $Agent)) {
        $os = 'OSF1';
    } elseif (preg_match('IRIX', $Agent)) {
        $os = 'IRIX';
    } elseif (preg_match('FreeBSD', $Agent)) {
        $os = 'FreeBSD';
    } elseif ($os == '') {
        $os = 'Unknown';
    }
    return $os;
}

function real_server_ip()
{
    static $serverip = NULL;
    if ($serverip !== NULL) {
        return $serverip;
    }
    if (isset($_SERVER)) {
        if (isset($_SERVER['SERVER_ADDR'])) {
            $serverip = $_SERVER['SERVER_ADDR'];
        } else {
            $serverip = '0.0.0.0';
        }
    } else {
        $serverip = getenv('SERVER_ADDR');
    }
    return $serverip;
}